import { parseToObject } from '@/utils';
import { formatDate } from '@/utils/util';
import { TASK_STATUS, TaskStatusEnum } from '@/constants/index';
import { INITIAL_FORM_QUERY_VALUE } from '../../constants';

export default {
  name: 'task-log-header-form',
  props: {
    topChannelList: {
      type: Array,
      default: () => [],
    },
    // 表单默认值
    defaultFormQueryValueStr: String,
    querying: {
      type: Boolean,
      default: false,
    },
  },

  data() {
    return {
      formQuery: { ...INITIAL_FORM_QUERY_VALUE },
      taskStatusList: TASK_STATUS.filter(item => item.id !== TaskStatusEnum.CANCEL),
      labelWidth: '80px',
      okBtnLoading: false,
      taskTime: [],
    };
  },
  watch: {
    // 监听传入的表单默认值变化
    defaultFormQueryValueStr(newValue) {
      if (!newValue) {
        return;
      }
      this.formQuery = {
        ...this.formQuery,
        ...parseToObject(newValue),
      };
    },
  },
  methods: {
    onQuery() {
      this.$emit('onQuery', { ...this.formQuery });
    },
    reset() {
      this.initTime();
      this.formQuery = { ...INITIAL_FORM_QUERY_VALUE };
    },
    timeChange(time) {
      if (time) {
        this.formQuery = { ...this.formQuery, timeRange: { startTime: this.taskTime[0], endTime: this.taskTime[1] } };
      } else {
        this.taskTime = [];
        this.formQuery = { ...this.formQuery, timeRange: null };
      }
    },
    initTime() {
      const startDate = new Date();
      startDate.setDate(startDate.getDate() - 1);
      const endDate = new Date();
      endDate.setDate(endDate.getDate() + 1);
      this.taskTime = [formatDate(startDate, 'yyyy-MM-dd 00:00:00'), formatDate(endDate, 'yyyy-MM-dd 00:00:00')];
      this.formQuery = { ...this.formQuery, timeRange: { startTime: this.taskTime[0], endTime: this.taskTime[1] } };
    },
  },
  async mounted() {
    this.initTime();
    this.onQuery();
  },
};
